/******************************************************************
 * Created by: Goh Chee Hong                                       
 * Date of Creation: 1/1/2009
 * Date of last modification: 31/1/2009
 * 
 *                                                                
 * Description: An abstract class
 ******************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Pbdms.BusinessLogic;
using Pbdms.Common.Interfaces;

using Pbdms.NetworkDealer;
using Pbdms.Common.DataObject;

namespace Pbdms.BusinessLogic.FileTransfer
{
    public abstract  partial class FileTransferGui : UserControl
    {
        public FileTransferGui()
        {
            InitializeComponent();
        }



        protected System.IO.FileInfo fileInfo;
        /// <summary>
        /// friend public key id
        /// </summary>
        protected string keyID;
        /// <summary>
        /// friend name
        /// </summary>
        protected string friendName;
        /// <summary>
        /// saved file location
        /// </summary>
        protected string fileName;
        /// <summary>
        /// network controller
        /// </summary>
        protected Network.NetworkController networkController;
        /// <summary>
        /// indaicate whether it is finished
        /// </summary>
        protected Boolean finished = false;

        /// <summary>
        /// the connection passed from Network Dealer
        /// </summary>
        protected AuthTcpClient authTcpClient;


        /// <summary>
        ///  the invoke method;
        /// </summary>
        /// <param name="args"></param>
        public delegate void DelegateWorker(string args);

        public EventHandler CancelClicked;
        public delegate void FinishedEventHandler(object sender);
        public FinishedEventHandler FinishedEvent;

        public abstract void start();
      

        public  Boolean Finished()
        {
            return finished;

        }
        public void changeName(string keyID, string oldName, string newName)
        {
            if (this.keyID.Equals(keyID))
            {
                lblUser.Text = newName;
            }
        }
        public string KeyID()
        {
            return keyID;
        }
        /// <summary>
        /// cancel event of the button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void btnCancel_Click(object sender, EventArgs e)
        {
            if (authTcpClient.Connected() == true)
            {
                authTcpClient.Close();
            }
            
            if (CancelClicked != null)
            {
                CancelClicked(this, EventArgs.Empty);
            }
        }

  
        /// <summary>
        /// the status of the transfer
        /// </summary>
        /// <param name="args"></param>
        protected void displayStatus(string args)
        {
            if (InvokeRequired)
            {
                DelegateWorker delegateWorker = new DelegateWorker(displayStatus);
                this.BeginInvoke(delegateWorker, new object[] { args });
            }
            else
            {
                this.lblStatus.Text = args;
            }
        }
        /// <summary>
        /// progress of the transfer
        /// </summary>
        /// <param name="args"></param>
        protected void displayProgress(string args)
        {
            if (InvokeRequired)
            {
                DelegateWorker delegateWorker = new DelegateWorker(displayProgress);
                this.BeginInvoke(delegateWorker, new object[] { args });
            }
            else
            {
                progressBar1.Value = int.Parse(args);
            }
        }
        /// <summary>
        /// finished method
        /// </summary>
        /// <param name="args"></param>
        protected void Finished(string args)
        {
            if (InvokeRequired)
            {
                DelegateWorker delegateWorker = new DelegateWorker(Finished);
                this.BeginInvoke(delegateWorker, new object[] { args });
            }
            else
            {
                btnCancel.Text = "Done";
            }
        }
    }
}
